/**
 * @Author:  renyc
 * @Date: 2022年12月2日15:56:28
 */

import * as Cesium from 'cesium/Cesium';
import Viewer from "../viewer/Viewer"

class Transformer{

    /**
     * 屏幕坐标转换为经纬度地理坐标
     * @param {*} screenPos 屏幕坐标
     * @param {Viewer} viewer    Viewer对象
     * @returns {Position} lng,lat,alt
     */
    static screen2Geographic(screenPos, viewer){
        let position = undefined;
        //1. 将屏幕坐标转换为Cesium的球面坐标
        let cartesian = viewer.camera.pickEllipsoid(
                                    screenPos,
                                    viewer.scene.globe.ellipsoid
                                );
        if (Cesium.defined(cartesian)) {
            //2. 将笛卡尔坐标转换为球面坐标
            let cartographic = viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian);
            if(Cesium.defined(cartographic)) {
                //6. 将弧度转换为角度
                position = {
                    lng : Cesium.Math.toDegrees(cartographic.longitude).toFixed(6),
                    lat : Cesium.Math.toDegrees(cartographic.latitude).toFixed(6),
                    alt : viewer.camera.positionCartographic.height.toFixed(6)
                };
			}
        }
        return position;
    }

   /**
    * 地理坐标转换为笛卡尔坐标
    * @param position
    * @returns {Cartesian3}
    */
  static transformWGS84ToCartesian(position) {
    return position
      ? Cesium.Cartesian3.fromDegrees(
          position.lng,
          position.lat,
          position.alt,
          Cesium.Ellipsoid.WGS84
        )
      : Cesium.Cartesian3.ZERO
  }
}

export default Transformer